var lenglianguiji = ["lenglian01", "lenglian02"]
var gongsiname = [];
var a = document.getElementById("sel").value;
var L1 = 0; //数据长度
var L2 = 0;
var c = 0; //小车的起始位置
function getData_guiji(a) {
	if (a == "冷链1") {
		var b = lenglianguiji[0];
	}
	if (a == "冷链2") {
		var b = lenglianguiji[1];
	}
	var result;
	$.ajax({
		type: "POST",
		async: false,
		url: "http://211.68.191.30:8090/coldchain/get/guiji",
		data: {
			lenglianbianma: b,
		},
		dataType: "json",
		success: function(data) {
			console.log("轨迹数据");
			console.log(data);
			result = data;
		},
		error: function(message) {
			alert("轨迹请求数据失败!");
		}
	});
	return result;
}

function setData_guiji(data, chart) {
	//console.log("配置表" + meter);
	shijian = [];
	yibiao1 = [];
	yibiao2 = [];
	weizhi1 = [];
	if (data) {
		for (i = 0; i < i_guiji; i++) { //data.length
			shijian.push(data[i].shijian);
			t1 = shijian[i].slice(0, 10);
			t2 = shijian[i].slice(11, 19);
			shijian[i] = t1 + '\n' + t2;

			yibiao2.push(data[i].wendu);

			weizhi1.push(data[i].weizhi);
			console.log(weizhi1[0]);

			gongsiname.push(data[i].dizhi);
			console.log('dizhi');
			console.log(gongsiname[1]);
		}
	}
	console.log("温度" + yibiao1);
	chart.hideLoading();
	//填入参数
	chart.setOption({
		xAxis: {
			data: shijian
		},
		series: [{
				// 根据名字对应到相应的系列
				// name: '仪表1',
				data: yibiao1
			},
			{
				// 根据名字对应到相应的系列
				// name: '仪表2',
				data: yibiao2
			},
		],
	});
	//}
}

// var json_data =[];
// for(i=0,i<weizhi.length,i++){
// }
var weizhi = [];
var guiji = [];

function qingqiuguiji(a) {
	if (a == "冷链1") {
		var b = lenglianguiji[0];
	}
	if (a == "冷链2") {
		var b = lenglianguiji[1];
	}
	var result;
	$.ajax({
		type: "POST",
		async: false,
		url: "http://211.68.191.30:8090/coldchain/get/guiji",
		data: {
			lenglianbianma: b,
		},
		dataType: "json",
		success: function(data) {
			result = data;
			// 清空数组
			guiji = [];
			//console.log(data);
			// 遍历返回结构
			for (var i = 0; i < i_guiji; i++) {
				// 转换坐标字符串为数组
				weizhi = [];
				weizhi.push(parseFloat(data[i].weizhi.split(",")[0]), parseFloat(data[i].weizhi.split(",")[1]));
				guiji.push(weizhi);
			}
			//console.log(guiji);
			//console.log(weizhi);
		},
		error: function(e) {
			console.log(e.statusText);
		},
	});
	return result;
}

//下拉框选择
function gradeChange(tx) {
	map = new AMap.Map("container", {
		resizeEnable: true,
		center: guiji[0],
		zoom: 12
	});
	i_guiji=1;
	console.log(tx);
	//获取DOM元素生成echarts对象
	var meter3Chart = echarts.init(document.getElementById("meter3"));
	//初始化配置
	meter3Chart.setOption(option3);
	//仪表
	var meter3 = "温度";
	//显示加载动画效果
	meter3Chart.showLoading();
	var a = document.getElementById("sel").value;
	var dataDay = getData_guiji(a);
	setData_guiji(dataDay, meter3Chart);
	qingqiuguiji(a);
	guijihuoqu(guiji);
}
function gradeChange1(tx) {
	console.log(tx);
	//获取DOM元素生成echarts对象
	var meter3Chart = echarts.init(document.getElementById("meter3"));
	//初始化配置
	meter3Chart.setOption(option3);
	//仪表
	var meter3 = "温度";
	//显示加载动画效果
	meter3Chart.showLoading();
	var a = document.getElementById("sel").value;
	var dataDay = getData_guiji(a);
	setData_guiji(dataDay, meter3Chart);
	qingqiuguiji(a);
	guijihuoqu(guiji);
}
//轨迹数据放进linarr	
var marker, lineArr = [];
var lineArr1 = [];
var map = new AMap.Map("container", {
	resizeEnable: true,
	center: guiji[0],
	zoom: 12
});


function guijihuoqu(guiji) {
	lineArr = []; //点击函数内容变空
	marker,
	data = [];
	marker,
	lineArr = guiji;
	for (var i = 0; i < i_guiji; i++) {
		lineArr1 = [];
		lineArr1.push(guiji[i - 1]);
		lineArr1.push(guiji[i]);
		//console.log(lineArr1);
	}

	// 添加一些分布不均的点到地图上,地图上添加三个点标记，作为参照
	for (var i = 0; i < i_guiji; i++) {
		var marker1 = new AMap.Marker({
			map: map,
			icon: 'd13.ico',
			size: new AMap.Size(2, 2),
			position: guiji[i],
			offset: new AMap.Pixel(-13, -30)
		});
		marker1.setTitle(gongsiname[i]);
	}
	//创建小汽车marker
	var i = i_guiji
	marker = new AMap.Marker({
		map: map,
		position: guiji[i - 1],
		icon: "https://webapi.amap.com/images/car.png",
		offset: new AMap.Pixel(-26, -13),
		autoRotation: true,
		angle: -90,
	});
	//绘制轨迹
	var polyline = new AMap.Polyline({
		map: map,
		path: lineArr,
		//showDir: true,
		strokeColor: "#28F", //线颜色
		// strokeOpacity: 1,     //线透明度
		strokeWeight: 6, //线宽
		// strokeStyle: "solid"  //线样式
	});
	var passedPolyline = new AMap.Polyline({
		map: map,
		// path: lineArr,
		strokeColor: "#28F", //线颜色
		// strokeOpacity: 1,     //线透明度
		strokeWeight: 6, //线宽
		// strokeStyle: "solid"  //线样式
	});
	// 同时引入工具条插件，比例尺插件和鹰眼插件
	AMap.plugin([
		'AMap.ToolBar',
		'AMap.Scale',
		'AMap.OverView',
		'AMap.MapType',
		'AMap.Geolocation',
	], function() {
		// 在图面添加工具条控件，工具条控件集成了缩放、平移、定位等功能按钮在内的组合控件
		map.addControl(new AMap.ToolBar());
		// 在图面添加比例尺控件，展示地图在当前层级和纬度下的比例尺
		map.addControl(new AMap.Scale());
		// 在图面添加鹰眼控件，在地图右下角显示地图的缩略图
		map.addControl(new AMap.OverView({
			isOpen: true
		}));
		// 在图面添加类别切换控件，实现默认图层与卫星图、实施交通图层之间切换的控制
		map.addControl(new AMap.MapType());
		// 在图面添加定位控件，用来获取和展示用户主机所在的经纬度位置
	});
	marker.on('moving', function(e) {
		passedPolyline.setPath(e.passedPath);
	});
	map.setFitView();
}
//开始
function startAnimation() {
	marker.moveAlong(lineArr1, 300); //800
}
//暂停
function pauseAnimation() {
	marker.pauseMove();
}
//继续
function resumeAnimation() {
	marker.resumeMove();
}
//停止
function stopAnimation() {
	marker.stopMove();
}
var i_guiji = 1;

function shuaxin() {
	var a = document.getElementById("sel").value;
	var dataDay = getData_guiji(a);
	map.remove(marker);
	// if(L1==0){
	// 	L1=dataDay.length;
	// }
	// L2=dataDay.length;
	// console.log(L2);
	// console.log('c:'+c);
	// if(L2>L1){    //如果获取到新的数据，重新绘制
	// c=L1-1;
	// console.log("c:"+c);
	// 	L1=dataDay.length;
	var tx1 = document.getElementById("sel").value;
	gradeChange1(tx1);
	//marker.moveAlong(lineArr, 300); //800
	if (i_guiji < dataDay.length) {
		i_guiji = i_guiji + 1;
	}

	var a = document.getElementById("sel").value;
	var data = qingqiuguiji(a);
	if (i_guiji < dataDay.length) {
		startAnimation();
	}
	
	
	// }

}
// function shuaxin2() {
// 	var a = document.getElementById("sel").value;
// 	var data = qingqiuguiji(a);
// 	//console.log(data);
// 	if (i_guiji <= data.length) {
// 		i_guiji = i_guiji + 1;
// 	}
// 	//gradeChange();
// 	gradeChange2()
// 	startAnimation() 
// }
